package com.ca.ridersclubapplication.request;

import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.data.entity.BooleanEntity;
import com.ca.ridersclubapplication.request.dto.LoginResult;
import com.ca.ridersclubapplication.request.dto.Result;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public class LoginRequester {

    public static String URL = "login/";

    public static final LoginAPI api;

    static {
        api = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL + URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(LoginAPI.class);
    }

    public interface LoginAPI {

        @POST("password")
        @FormUrlEncoded
        Call<Result<LoginResult>> loginByPassword(
                @Field("email") String email,
                @Field("password") String password);

        @POST("captcha")
        @FormUrlEncoded
        Call<Result<LoginResult>> loginByCaptcha(
                @Field("email") String email,
                @Field("code") String code);

        @POST("update")
        @FormUrlEncoded
        Call<Result<String>> updatePassword(
                @Field("email") String email,
                @Field("code") String code,
                @Field("password") String password);
    }

    public static void loginByPassword(Activity activity, TextView tipView, String email, String password) {
        loginByPassword(activity, tipView, email, password, null);
    }

    public static void loginByPassword(Activity activity, TextView tipView, String email, String password, AfterExecutor<LoginResult> executor) {
        api.loginByPassword(email, password)
                .enqueue(new APICallback<LoginResult>(activity, tipView, executor) {
                    @Override
                    public void onResponse(@NonNull Call<Result<LoginResult>> call, @NonNull Response<Result<LoginResult>> response) {
                        super.onResponse(call, response);
                        if (isSuccess()) {
                            //  登录成功，将认证信息存入本机
                            DataHandler.login(getActivity(), getData());
                            Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public static void loginByCaptcha(Activity activity, TextView tipView, String email, String code) {
        loginByCaptcha(activity, tipView, email, code, null);
    }

    public static void loginByCaptcha(Activity activity, TextView tipView, String email, String code, AfterExecutor<LoginResult> executor) {
        api.loginByCaptcha(email, code)
                .enqueue(new APICallback<LoginResult>(activity, tipView, executor) {
                    @Override
                    public void onResponse(@NonNull Call<Result<LoginResult>> call, @NonNull Response<Result<LoginResult>> response) {
                        super.onResponse(call, response);
                        if (isSuccess()) {
                            //  登录成功，将认证信息存入本机
                            DataHandler.login(getActivity(), getData());
                            Toast.makeText(getActivity(), "登录成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public static void updatePassword(Activity activity, TextView tipView, String email, String code, String password) {
        updatePassword(activity, tipView, email, code, password, null);
    }

    public static void updatePassword(Activity activity, TextView tipView, String email, String code, String password, AfterExecutor<String> executor) {
        api.updatePassword(email, code, password)
                .enqueue(new APICallback<>(activity, tipView, executor));
    }
}
